package com.sky.service.impl;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.result.Result;
import com.sky.service.ShopCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {
    @Autowired
    private ShopCartMapper shopCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public Result add(ShoppingCartDTO shoppingCartDTO) {
        if(shoppingCartDTO.getSetmealId() == null&&shoppingCartDTO.getDishId()==null){
            return Result.error("信息为空");
        }
        ShoppingCart shoppingCartE = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCartE);
        shoppingCartE.setUserId(4L);
        ShoppingCart shoppingCart= shopCartMapper.getById(shoppingCartE);
        //判断添加的是套餐还是菜品
        if(shoppingCartDTO.getSetmealId()!=null&&shoppingCartDTO.getSetmealId()!=0){
            //是套餐
            //查询是否已有数据在购物车
            SetmealVO setmealVO = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            if(shoppingCart==null){
                //构建购物卡对象
                shoppingCartE.setName(setmealVO.getName());
                shoppingCartE.setNumber(1);
                shoppingCartE.setAmount(setmealVO.getPrice());
                shoppingCartE.setCreateTime(LocalDateTime.now());
                shopCartMapper.add(shoppingCartE);
            }else{
                shopCartMapper.updateCountById(shoppingCartE);
            }
        }else {
            //是菜品
            DishVO dishVO = dishMapper.getById(shoppingCartDTO.getDishId());
            if(shoppingCart==null){
                shoppingCartE.setName(dishVO.getName());
                shoppingCartE.setNumber(1);
                shoppingCartE.setAmount(dishVO.getPrice());
                shoppingCartE.setCreateTime(LocalDateTime.now());
                shopCartMapper.add(shoppingCartE);
            }else{
                shopCartMapper.updateCountById(shoppingCartE);
            }
        }
        return Result.success();
    }

    @Override
    public List<ShoppingCart> list() {

        return shopCartMapper.listById(4L);
    }

    @Override
    public void clean() {
        shopCartMapper.clean(4L);
    }

    @Override
    public void delOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(4L);
        if((shoppingCartDTO.getSetmealId()!=null&&shoppingCartDTO.getSetmealId()!=0)
        ||(shoppingCartDTO.getDishId()!=null&&shoppingCartDTO.getDishId()!=0)){
            ShoppingCart shopCart = shopCartMapper.getById(shoppingCart);
            if(shopCart.getNumber()>1){
                shopCartMapper.reduceCount(shoppingCart);
            }else{
                shopCartMapper.delOne(shopCart.getId());
            }
        }
    }
}
